package middleware

import (
	"fairysunny.com/gin-example/util"
	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
)

func CheckRole(min int) gin.HandlerFunc {
	return func(c *gin.Context) {
		session := sessions.Default(c)
		id := session.Get("user-id")
		if id == nil {
			util.ResErr(c, "未登录")
			c.Abort()
			return
		}
		if id.(int) < min {
			util.ResErr(c, "权限不足")
			c.Abort()
			return
		}
		c.Next()
	}
}
